package org.dromara.wmscloud.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.wmscloud.bean.WarehousePdfData;
import org.dromara.wmscloud.common.IAsyncService;
import org.dromara.wmscloud.utils.PDFService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.wmscloud.domain.vo.BaseWarehouseVo;
import org.dromara.wmscloud.domain.bo.BaseWarehouseBo;
import org.dromara.wmscloud.service.IBaseWarehouseService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 仓库
 *
 * @author hou
 * @date 2025-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wmsCloud/warehouse")
public class BaseWarehouseController extends BaseController {

    private final IBaseWarehouseService baseWarehouseService;
    private final PDFService pdfService;
    private final IAsyncService asyncService;
    /**
     * 查询仓库列表
     */
    @SaCheckPermission("base:warehouse:list")
    @GetMapping("/list")
    public TableDataInfo<BaseWarehouseVo> list(BaseWarehouseBo bo, PageQuery pageQuery) {
        return baseWarehouseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出仓库列表
     */
    @SaCheckPermission("base:warehouse:export")
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseWarehouseBo bo, HttpServletResponse response) {
        List<BaseWarehouseVo> list = baseWarehouseService.queryList(bo);
        ExcelUtil.exportExcel(list, "仓库", BaseWarehouseVo.class, response);
    }

    /**
     * 获取仓库详细信息
     *
     * @param warehouseId 主键
     */
    @SaCheckPermission("base:warehouse:query")
    @GetMapping("/{warehouseId}")
    public R<BaseWarehouseVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long warehouseId) {
        return R.ok(baseWarehouseService.queryById(warehouseId));
    }

    /**
     * 新增仓库
     */
    @SaCheckPermission("base:warehouse:add")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseWarehouseBo bo) {
        return toAjax(baseWarehouseService.insertByBo(bo));
    }

    /**
     * 修改仓库
     */
    @SaCheckPermission("base:warehouse:edit")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseWarehouseBo bo) {
        return toAjax(baseWarehouseService.updateByBo(bo));
    }

    /**
     * 删除仓库
     *
     * @param warehouseIds 主键串
     */
    @SaCheckPermission("base:warehouse:remove")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{warehouseIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] warehouseIds) {
        return toAjax(baseWarehouseService.deleteWithValidByIds(List.of(warehouseIds), true));
    }

    /**
     * 打印仓库
     *
     *  @param warehouseId 主键串
     */
    @SaCheckPermission("base:warehouse:print")
    @Log(title = "仓库", businessType = BusinessType.OTHER)
    @GetMapping("printWarehouse/{warehouseId}")
    public void printLocation(HttpServletResponse response, @PathVariable Long warehouseId) throws Exception{
        OutputStream out = response.getOutputStream();
        response.setContentType("application/pdf");
        BaseWarehouseVo warehouse = baseWarehouseService.queryById(warehouseId);
        if(warehouse != null){
            WarehousePdfData warehousePdfData = new WarehousePdfData();
            warehousePdfData.setWarehouseCode(warehouse.getWarehouseCode());
            warehousePdfData.setWarehouseName(warehouse.getWarehouseName());
            InputStream in = new FileInputStream(new File(pdfService.printWareHouse(warehousePdfData)));
            int len = 0;
            byte[] buffer = new byte[1024];
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }

            in.close();
        }
    }
}
